<?php
/**
 * @Author:         zk
 * @DateTime:       18-9-19 下午2:50
 * @Description:    冲洗 anhui 库中 maintenance_supplier 供应商的“logistics_freight-物流运费”数据     区域
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
set_time_limit(0);
DB::connection()->disableQueryLog();      //关闭日志

/*
 * 供应商等级   区域
 * logistics_freight	物流运费(1:含运费;2:不含运费;3:按区域设置;4:按门店设置;)
 * 文件格式 [中间分隔符为 #]    文件编码 utf-8
 * 供应商ID#供应商名称#多个城市ID用‘英文逗号’隔开#物流运费(1:含运费;2:不含运费;)
 * 例如：
 * 1473#北京东鹏汽车商行（压缩机）#1,11#1
 * 1476#北京福虎锁具有限责任公司（钥匙）#4,28#0
 */
//本地环境
//$txt_name = '\supplier_txt\logistics_freight_area.txt';     //供应商物流运费   anhui\api\public\supplier_txt\logistics_freight_area.txt
$txt_name = '/supplier_txt/logistics_freight_area.txt';     //供应商物流运费   anhui\api\public\supplier_txt\logistics_freight_area.txt
$path_name = public_path().''.$txt_name;
$content = file_get_contents($path_name);
ini_set('memory_limit', '-1');//不要限制Mem大小
$content_arr = explode("\n",$content);
$now_time = date('Y-m-d H:i:s', time());

$error_arr = [];    //有问题的数据数组
$area_data = [];    //添加新的区域配置信息
$supplier_id_arr = [];  //供应商ID数组
$supplier_id_str = '';  //供应商ID字符串
$sql = "UPDATE maintenance_supplier SET updated_at='".$now_time."', logistics_freight = CASE ";
foreach ($content_arr as $k => $v) {
    $line_arr = explode('#', $v);
    //判断数组的长度
    if(count($line_arr) == 4){
        $supplier_id = $line_arr[0];
        $value = 3;   //物流运费(1:含运费;2:不含运费;3:按区域设置;4:按门店设置;)
        $logistics_freight = $line_arr[3];
        
        $supplier_id_arr[] = $supplier_id;
        $supplier_id_str .= $supplier_id.',';
        $sql .= 'WHEN id='.$supplier_id.' THEN '.$value.' ';
        
        //供应商物流运费“按区域设置”的扩展数据
        $city_id_arr = explode(',', $line_arr[2]);
        if(!empty($city_id_arr)){
            foreach ($city_id_arr as $city_id) {
                $temp = [
                    'supplier_id' => $supplier_id,
                    'city_id' => $city_id,
                    'logistics_freight' => $logistics_freight,
                    'created_at' => $now_time,
                    'created_by' => 0,  //系统
                ];
                $area_data[] = $temp;
            }
        }
    }else{
        $error_arr[] = $v;  //不符合的数组
    }
}
$sql .= 'ELSE logistics_freight END WHERE id IN ('. rtrim($supplier_id_str, ',').');';

try {
    DB::beginTransaction();
    
    $res = DB::update(DB::raw($sql));
    if(!$res){
        DB::rollback();
        Log::info("供应商物流运费 error 执行的数据条数为res: ".$res." "); 
        echo "供应商物流运费 error 执行的数据条数为res: ".$res." \r\n";
        return;
    }
    
    //删除原有信息
    MaintenanceSupplierLogisticsFreightArea::whereIn('supplier_id', $supplier_id_arr)->delete();
    
    //添加新的区域配置信息
    $res_area_insert = MaintenanceSupplierLogisticsFreightArea::insert($area_data);
    if(!$res_area_insert){
        DB::rollback();
        Log::info("供应商物流运费 区域配置信息添加失败 error "); 
        echo "供应商物流运费 区域配置信息添加失败 error \r\n";
        return;
    }
    DB::commit();
    Log::info("供应商物流运费 按区域配置设置成功 ok 执行的数据条数为res: ".$res." ");
    echo "供应商物流运费 按区域配置设置成功 ok 执行的数据条数为res: ".$res." \r\n";
}catch(Exception $e){
    DB::rollback();
    Log::info("供应商物流运费 按区域配置设置失败 error ", array("exception" => $e)); 
    echo "供应商物流运费 按区域配置设置失败 error \r\n";
    return;
}

//记录 异常的信息
if(!empty($error_arr)){
    $error_num = count($error_arr);
    $json_str = json_encode($error_arr);
    Log::info("供应商物流运费按区域配置设置 异常的数据数量 num:".$error_num.",异常的数据为 ".$json_str." "); 
    echo "供应商物流运费按区域配置设置 异常的数据数量 num:".$error_num.",异常的数据为 ".$json_str." \r\n";
}